package com.kedeng.yangmuyi.service.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kedeng.yangmuyi.common.util.MD5Util;
import com.kedeng.yangmuyi.controller.LoginController;
import com.kedeng.yangmuyi.dto.LoginUserDTO;
import com.kedeng.yangmuyi.dto.UserProfileDTO;
import com.kedeng.yangmuyi.exception.CheckLoginUserDataException;
import com.kedeng.yangmuyi.exception.MD5Exception;
import com.kedeng.yangmuyi.service.LoadUserDataService;
import com.kedeng.yangmuyi.service.LoginService;

/**
 * 
 * Hash password with MD5, then validate the login info is correct.
 * 
 * @author: rqian
 * @version: 1.0, Apr 26, 2012
 */
@Service("loginService")
@Transactional
public class LoginServiceImpl implements LoginService {

    private static final Logger logger = LoggerFactory.getLogger(LoginServiceImpl.class);

    @Resource
    private LoadUserDataService loadUserDataService;



    @Override
    public boolean checkLoginUserData(LoginUserDTO user) {
        logger.debug("LoginServiceImpl.checkLoginUserData(): enter!");
        logger.debug("LoginServiceImpl.checkLoginUserData(): user = " + user);
        if (null == user) {
            return false;
        }
        String passwdMD5 = "";
        try {
            passwdMD5 = MD5Util.hashStrWithMD5(user.getPassword());
        } catch (MD5Exception e) {
            throw new CheckLoginUserDataException(passwdMD5 + " | " + e.getErrorMessage());
        }

        if (passwdMD5 == null) {
            throw new CheckLoginUserDataException();
        }

        user.setPasswordWithMD5(passwdMD5);

        UserProfileDTO profile = loadUserDataService.loadUserProfileWithLoginUserData(user);
        if (profile == null) {
            return false;
        }
        logger.debug("LoginServiceImpl.checkLoginUserData(): exit!");
        return true;
    }



    @Override
    public boolean registerUser(UserProfileDTO profile) {
        // TODO Auto-generated method stub
        return false;
    }

}
